package org.jxf.gps.jt808.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import javax.servlet.http.HttpSession;

import com.jfinal.plugin.activerecord.Model;

public class GpsServer {
	
	//public static final String LAST_POINT = "last";
	public static final String TERN_NEW_INFO = "TERN_NEW_INFO";
	public static final String MO_MSG = "moMsg";
	
	public static final int TERMINAL_TIMEOUT = 1000*60*3;
	public static final int HTTPSESSION_TIMEOUT = 1000*60*30;
	public static final int CHECK_INTERVAL = 1000*1;
	
	public static int tcpConnCount = 0;
	public static String tcpConnCountLock = "";//防止计数不准

	public static int tcpMsgRecvCount = 0;
	public static int tcpMsgQCount = 0;
	public static String tcpMsgRecvCountLock = "";//防止计数不准
	
	public static int tcpMsgRevErrCount = 0;
	
	public static int tcpExceptionCaught = 0;
	
	public static Queue<Model<?>> dbQueue = new ConcurrentLinkedQueue();
	
	/**
	 * 在线车辆及其对应的最新数据
	 */
	public static final Map<String,Map> onlineTerminals = new HashMap<String,Map>();	
	
	/**
	 * WEB登录账号所属group及其session
	 */
	public static final Map<String,List<HttpSession>> orgSessionMap = new HashMap<String,List<HttpSession>>();
	
	/**
	 * session及其对应的消息列表
	 */
	public static final Map<HttpSession,List<Map>> sessionMessage = new HashMap<HttpSession,List<Map>>();
	
	/**
	 * 通过mdn找到对应的已经Login的账号，并对其发送告警消息。
	 * @param mdn
	 * @param m
	 */
	public static void send2LoginSession(String groupPath,Map m){
		
		Iterator itr2 = GpsServer.orgSessionMap.keySet().iterator();
		List orgList = new ArrayList();
		while(itr2.hasNext()){
			Object org = itr2.next();
			if (groupPath.startsWith(org.toString())){
				List<HttpSession> list = GpsServer.orgSessionMap.get(org);
				for (int i = 0 ; list != null && i < list.size() ; i++){
					 List<Map> listMsg = GpsServer.sessionMessage.get(list.get(i));
					 if (listMsg == null){
						 listMsg = new ArrayList<Map>();
					 }

					 listMsg.add(m);
					 GpsServer.sessionMessage.put(list.get(i), listMsg);
				}
			}
		}			
	}		

}

